package de.hohenheim.modell;


import java.util.ArrayList;
import java.util.Iterator;

import de.hohenheim.view.FigureFactory;
import de.hohenheim.view.map.RoomMap;
import de.hohenheim.view.mobile.ForkTruckFigure;
import de.hohenheim.view.mobile.animation.listeners.AnimationFinishedEvent;
import de.hohenheim.view.mobile.animation.listeners.AnimationListener;
import de.hohenheim.view.mobile.animation.listeners.AnimationStartedEvent;
import de.hohenheim.view.room.RoomFigure;

public class ForkTruck {
	ForkTruckFigure figure; 
	ArrayList<State> my_blocking_states_list = new ArrayList<State>();
	int gabelstaplerID;
	double gewicht;
	
	public ForkTruck(RoomMap map, RoomFigure in_room, int fork_truck_id, double tragegewicht) {
		gabelstaplerID = fork_truck_id;
		gewicht = tragegewicht;
		figure = FigureFactory.createForkTruckFigure(map, in_room, fork_truck_id, this);
	    figure.addAnimationListener(new AnimationListener() {
			
			public void animationFinished(AnimationFinishedEvent e) {
			  ForkTruckFigure fig=(ForkTruckFigure)e.getSource();	
			  System.out.print("Fork_truck " + fig.getForkTruckId()+" is finished with ");
			  switch (e.getAnimationtype()) {
			  	case AnimationFinishedEvent.BUSY_FINISHED:
			  		System.out.println("waiting ...");
			  		break;
			  	case AnimationFinishedEvent.LOADING_FINISHED:
			  		System.out.println("loading ...");
			  		if(fig.getRoomFigure()!=null) {
						//State state = State.statemap.get(fig.getRoomFigure().getName());
						for(int i=0; i<my_blocking_states_list.size();i++) {
							my_blocking_states_list.get(i).setState(State.UNBLOCKED);
						}
			  			my_blocking_states_list=new ArrayList<State>();			  			
					}
			  		break;
			  	case AnimationFinishedEvent.MOVE_FINISHED:
			  		System.out.println("moving ...");
			  		break;	
			  	case AnimationFinishedEvent.UNLOADING_FINISHED:
			  		System.out.println("unloading ...");
			  		if(fig.getRoomFigure()!=null) {
						//State state = State.statemap.get(fig.getRoomFigure().getName());
						for(int i=0; i<my_blocking_states_list.size();i++) {
							my_blocking_states_list.get(i).setState(State.UNBLOCKED);
						}
			  			my_blocking_states_list=new ArrayList<State>();			  			
					}
			  		break;
			  	default:
			  		break;
			  }
			}
		
			public void animationStarted(AnimationStartedEvent e) {
			  ForkTruckFigure fig=(ForkTruckFigure)e.getSource();	
			  System.out.print("Fork_truck " + fig.getForkTruckId()+" started ");
			  switch (e.getAnimationtype()) {
				case AnimationStartedEvent.BUSY_STARTED:
					System.out.println("waiting ...");
					break;
				case AnimationStartedEvent.LOADING_STARTED:
					System.out.println("loading ...");
					//Here we can block a room...
					if(fig.getRoomFigure()!=null) {
						for(int i=0; i<my_blocking_states_list.size();i++) {
							my_blocking_states_list.get(i).setState(State.UNBLOCKED);
						}
						my_blocking_states_list=new ArrayList<State>();
						
						State state = State.statemap.get(fig.getRoomFigure().getName());
						state.setState(State.BLOCKED);
						my_blocking_states_list.add(state);
					}
					break;
				case AnimationStartedEvent.MOVE_STARTED:
					System.out.println("moving ...");
					break;	
				case AnimationStartedEvent.UNLOADING_STARTED:
					System.out.println("unloading ...");
					//Here we can block a room...
					if(fig.getRoomFigure()!=null) {
						for(int i=0; i<my_blocking_states_list.size();i++) {
							my_blocking_states_list.get(i).setState(State.UNBLOCKED);
						}
						my_blocking_states_list=new ArrayList<State>();
						State state = State.statemap.get(fig.getRoomFigure().getName());
						state.setState(State.BLOCKED);
						my_blocking_states_list.add(state);
					}
					break;
				default:
					break;
			  }
			}		
	    });
	}
	
	public Iterator<State> getBlockedStates() {
		return this.my_blocking_states_list.iterator();
	}
	
	public double getTragegewicht() {
		return gewicht;
	}
}
